package com.brook.app.android.filepicker.core;

import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.MediaStore;
import androidx.core.content.MimeTypeFilter;
import android.webkit.MimeTypeMap;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * @author Brook
 * @time 2020-03-14 13:59
 */
public class VideoProvider extends DefaultFileProvider {

    @Override
    public boolean handleMimeType(String mimeType) {
        return MimeType.VIDEO.toString().equals(mimeType);
    }

    @Override
    protected List<File> getAllFile() {

        ContentResolver contentResolver = mFilePickerConfig.getApplicationContext().getContentResolver();

        List<File> videos = new ArrayList<>();

        String[] projection = new String[]{MediaStore.Video.VideoColumns.DATA};

        //asc 按升序排列
        //    desc 按降序排列
        //projection 是定义返回的数据，selection 通常的sql 语句，例如  selection=MediaStore.Video.VideoColumns.MIME_TYPE+"=? " 那么 selectionArgs=new String[]{"jpg"};
        Cursor cursor = contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Video.VideoColumns.DATE_MODIFIED + "  desc");

        if (cursor != null) {
            String filePath;
            while (cursor.moveToNext()) {
                filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Video.VideoColumns.DATA));
                File fileItem = new File(filePath);
                videos.add(fileItem);
            }
            cursor.close();
            cursor = null;
        }
        return videos;
    }
}
